#include "keyboard.h"
#include <dinput.h>

#define KEYDOWN(name, key) (name[key] & 0x80) 

void Keyboard::pingKeyboard()
{
	lastKeys = keys;
	
	if (KEYDOWN(buffer, DIK_LEFT))
		keys |= KEY_LEFT;
	else
		keys &= ~KEY_LEFT;

	if (KEYDOWN(buffer, DIK_UP))
		keys |= KEY_UP;
	else
		keys &= ~KEY_UP;

	if (KEYDOWN(buffer, DIK_DOWN))
		keys |= KEY_DOWN;
	else
		keys &= ~KEY_DOWN;

	if (KEYDOWN(buffer, DIK_RIGHT))
		keys |= KEY_RIGHT;
	else
		keys &= ~KEY_RIGHT;

	if (KEYDOWN(buffer, DIK_SPACE))
		keys |= KEY_SPACE;
	else
		keys &= ~KEY_SPACE;

	if (KEYDOWN(buffer, DIK_RETURN))
		keys |= KEY_ENTER;
	else
		keys &= ~KEY_ENTER;

	if (KEYDOWN(buffer, DIK_A))
		keys |= KEY_A;
	else
		keys &= ~KEY_A;

	if (KEYDOWN(buffer, DIK_B))
		keys |= KEY_B;
	else
		keys &= ~KEY_B;

	if (KEYDOWN(buffer, DIK_C))
		keys |= KEY_C;
	else
		keys &= ~KEY_C;

	if (KEYDOWN(buffer, DIK_D))
		keys |= KEY_D;
	else
		keys &= ~KEY_D;

	if (KEYDOWN(buffer, DIK_E))
		keys |= KEY_E;
	else
		keys &= ~KEY_E;

	if (KEYDOWN(buffer, DIK_F))
		keys |= KEY_F;
	else
		keys &= ~KEY_F;

	if (KEYDOWN(buffer, DIK_G))
		keys |= KEY_G;
	else
		keys &= ~KEY_G;

	if (KEYDOWN(buffer, DIK_H))
		keys |= KEY_H;
	else
		keys &= ~KEY_H;

	if (KEYDOWN(buffer, DIK_I))
		keys |= KEY_I;
	else
		keys &= ~KEY_I;


	if (KEYDOWN(buffer, DIK_J))
		keys |= KEY_J;
	else
		keys &= ~KEY_J;

	if (KEYDOWN(buffer, DIK_K))
		keys |= KEY_K;
	else
		keys &= ~KEY_K;

	if (KEYDOWN(buffer, DIK_L))
		keys |= KEY_L;
	else
		keys &= ~KEY_L;

	if (KEYDOWN(buffer, DIK_M))
		keys |= KEY_M;
	else
		keys &= ~KEY_M;

	if (KEYDOWN(buffer, DIK_N))
		keys |= KEY_N;
	else
		keys &= ~KEY_N;

	if (KEYDOWN(buffer, DIK_O))
		keys |= KEY_O;
	else
		keys &= ~KEY_O;

	if (KEYDOWN(buffer, DIK_P))
		keys |= KEY_P;
	else
		keys &= ~KEY_P;

	if (KEYDOWN(buffer, DIK_Q))
		keys |= KEY_Q;
	else
		keys &= ~KEY_Q;

	if (KEYDOWN(buffer, DIK_R))
		keys |= KEY_R;
	else
		keys &= ~KEY_R;

	if (KEYDOWN(buffer, DIK_S))
		keys |= KEY_S;
	else
		keys &= ~KEY_S;

	if (KEYDOWN(buffer, DIK_T))
		keys |= KEY_T;
	else
		keys &= ~KEY_T;

	if (KEYDOWN(buffer, DIK_U))
		keys |= KEY_U;
	else
		keys &= ~KEY_U;

	if (KEYDOWN(buffer, DIK_V))
		keys |= KEY_V;
	else
		keys &= ~KEY_V;

	if (KEYDOWN(buffer, DIK_W))
		keys |= KEY_W;
	else
		keys &= ~KEY_W;

	if (KEYDOWN(buffer, DIK_X))
		keys |= KEY_X;
	else
		keys &= ~KEY_X;

	if (KEYDOWN(buffer, DIK_Y))
		keys |= KEY_Y;
	else
		keys &= ~KEY_Y;

	if (KEYDOWN(buffer, DIK_Z))
		keys |= KEY_Z;
	else
		keys &= ~KEY_Z;

	g_lpDIDevice->GetDeviceState(sizeof(buffer),(LPVOID)&buffer);
}

bool Keyboard::isKeyHeld(key_t keyCheck)
{
	if(keys & keyCheck)
		return true;
	else
		return false;
}

bool Keyboard::isKeyUp(key_t keyCheck)
{
	if(keys & keyCheck)
		return false;
	else
		return true;
}

bool Keyboard::isKeyPressed(key_t keyCheck)
{
	if(keys & keyCheck && !(lastKeys & keyCheck))
		return true;
	else
		return false;
}

bool Keyboard::isKeyReleased(key_t keyCheck)
{
	if(lastKeys & keyCheck && !(keys & keyCheck))
		return true;
	else
		return false;
}